"""
@Project:study_platform
@File:serializer.py
@Author:韩晓雷
@Date:20:15
"""
import re
from rest_framework import serializers
from .models import *
from rest_framework.validators import ValidationError


# 轮播图的序列化器
class CarouselSer(serializers.ModelSerializer):
    class Meta:
        model = CarouselModel
        fields = "__all__"


# 用户序列化器
class UserSer(serializers.ModelSerializer):
    password = serializers.CharField(max_length=256, write_only=True, trim_whitespace=True)

    classs = serializers.PrimaryKeyRelatedField(queryset=ClasssModel.objects.all())
    classs_name = serializers.SerializerMethodField()

    def get_classs_name(self, obj):
        return obj.classs.name

    role = serializers.PrimaryKeyRelatedField(queryset=RoleModel.objects.all())
    role_name = serializers.SerializerMethodField()

    def get_role_name(self, obj):
        return obj.role.name

    class Meta:
        model = UserModel
        fields = "__all__"
        read_only_fields = ["id"]

    # 重写校验方法
    def validate(self, attrs):
        account = attrs.get("account")
        password = attrs.get("password")

        if not re.findall(r"[\w]{1,9}", account):
            raise ValidationError(f'{account}姓名由2到9为字母组成')

        if not re.findall(r"^[\w\W]{6,256}$", password):
            raise ValidationError(f'密码由6到20位数字、字母或下划线组成！')

        return attrs


# 个人信息序列化器
class UserInfoSer(serializers.ModelSerializer):
    gender = serializers.ChoiceField(choices=UserInfoModel.SEX, source="get_gender_display", read_only=True)

    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    user_account = serializers.SerializerMethodField()

    def get_user_account(self, obj):
        return obj.user.account

    class Meta:
        model = UserInfoModel
        fields = "__all__"
        read_only_fields = ["id"]

    # 重写校验方法
    def validate(self, attrs):
        name = attrs.get("name")

        if not re.findall(r"[\u4e00-\u9fa5]{1,9}", name):
            raise ValidationError(f'{name}姓名由2到9为汉字组成')

        return attrs


# 角色
class RoleSer(serializers.ModelSerializer):
    class Meta:
        model = RoleModel
        fields = "__all__"
        read_only_fields = ["id"]

    # 重写校验方法
    def validate(self, attrs):
        name = attrs.get("name")

        if not re.findall(r"[\u4e00-\u9fa5]{1,9}", name):
            raise ValidationError(f'{name}姓名由2到9为汉字组成')

        return attrs


# 登陆记录序列化器
class UserLoginSer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    user_account = serializers.SerializerMethodField()

    def get_user_account(self, obj):
        return obj.user.account

    class Meta:
        model = UserLoginModel
        fields = "__all__"
        read_only_fields = ["id"]


# 问候语序列化器
class GreetingsSer(serializers.ModelSerializer):

    class Meta:
        model = GreetingsModel
        fields = "__all__"


# 节日问候语序列化器
class FesGreetingsSer(serializers.ModelSerializer):

    class Meta:
        model = FesGreetingsModel
        fields = "__all__"


# 消息序列化器
class MsgSer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    user_account = serializers.SerializerMethodField()

    def get_user_account(self, obj):
        return obj.user.account

    users = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    users_account = serializers.SerializerMethodField()

    def get_users_account(self, obj):
        return obj.users.account

    class Meta:
        model = MessageModel
        fields = "__all__"
        read_only_fields = ["id"]


# 角色菜单序列化器
class RoleMenuSer(serializers.ModelSerializer):
    role = serializers.PrimaryKeyRelatedField(queryset=RoleModel.objects.all())
    role_name = serializers.SerializerMethodField()

    def get_role_name(self, obj):
        return obj.role.name

    menu = serializers.PrimaryKeyRelatedField(queryset=MenuModel.objects.all())
    menu_name = serializers.SerializerMethodField()

    def get_menu_name(self, obj):
        return obj.menu.name

    class Meta:
        model = RoleMenuModel
        fields = "__all__"
        read_only_fields = ["id"]


# 菜单序列化器
class MenuSer(serializers.ModelSerializer):

    class Meta:
        model = MenuModel
        fields = "__all__"
        read_only_fields = ["id"]

    # 重写校验方法
    def validate(self, attrs):
        name = attrs.get("name")

        if not re.findall(r"[\u4e00-\u9fa5]{1,9}", name):
            raise ValidationError(f'{name}菜单由2到9为汉字组成')

        return attrs


# 菜单序列化器
class SearchSer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    user_account = serializers.SerializerMethodField()

    def get_user_account(self, obj):
        return obj.user.account

    class Meta:
        model = SearchModel
        fields = "__all__"
        read_only_fields = ["id"]

